package com.gavin.model;

import java.io.Serializable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gavin.utils.Injector;
import com.gavin.utils.XMLElement;

/**
 * ------------------------------
 * 执行事务响应对象
 * ------------------------------
 * @author wdm（l311576@sina.com）  @date 2018年3月20日
 * @version 1.0
 */
@SuppressWarnings("unchecked")
public class Response  extends Model implements Serializable{
	private static Logger logger=LoggerFactory.getLogger(Response.class);

	private static final long serialVersionUID = 6060371848891779742L;
	
	public int result;
	public String message;
	public Object data;
	
	public Response() {}

	//-------------------------------------------------------------------------------------------
	public Response(int result) {
		this.result=result;
	}
	
	public Response(int result,String message) {
		this.result=result;
		this.message=message;
	}

	public int getResult() {
		return result;
	}

	public void setResult(int result) {
		this.result = result;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public boolean success(){
		return result==RetCode.SUCCESS.getState();
	}
	
	public boolean fail(){
		return result!=RetCode.SUCCESS.getState();
	}
	
	//static ---------------------------------------------------------------------------------------------------
	public static Response result(RetCode retCode,String message){
		return new Response(retCode.getState(),message);
	}
	public static Response result(int result, String message) {
		return new Response(result,message);
	}
	public static Response ok() {
		return Response.result(RetCode.SUCCESS, "success");
	}
	public static Response ifNull(Response response) {
		if(response==null){
			return Response.result(RetCode.REQUEST_FAIL, "请求失败");
		}
		return response;
	}
	//static ---------------------------------------------------------------------------------------------------	
	public Response log() {
		logger.info( "Response [result=" + result + ", message=" + message + ", data=" + this + "]");
		return this;
	}
	
	public void from(String modelXml){
		XMLElement xmlNode=new XMLElement();
		xmlNode.parseString(modelXml);
		Model model=new Model();
		model.fromXML(xmlNode);
		this.copy(model);
	}
	
	/**
	 * 根据别名获取bean
	 * @param clazz
	 * @param attrName
	 * @return
	 */
	public <M> M bean(Class<?> clazz,String attrName) {
		return (M) Injector.injectBean(clazz, attrName, this, true);
	}
	
	/**
	 * 根据别名获取list集合
	 * @param clazz
	 * @param attrName
	 * @return
	 */
	public <M> M list(Class<?> clazz,String attrName) {
		return (M) Injector.injectList(clazz, attrName,this, true);
	}
	
	public <M> M bean(Class<?> clazz) {
		return (M) Injector.injectBean(clazz, this, true);
	}
	
	public <M> M list(Class<?> clazz) {
		return (M) Injector.injectList(clazz, this, true);
	}

	public <M> M page(Class<?> clazz) {
		return (M) Injector.injectPage(clazz, this, true);
	}

	public Object getData() {
		if(this.data!=null){
			return data;
		}
		if(this.empty()){
			return null;
		}
		return this.toJson();
	}

	public void setData(Object data) {
		this.data = data;
	}

//	@Override
//	public String toString() {
//		logger.info("[result=" + result + ", message=" + message + "]");
//		return super.toString();
//	}
	
}
